package com.infra.micro.quartz.controller;

import com.infra.common.resp.Page;
import com.infra.common.resp.Response;
import com.infra.micro.quartz.model.req.QueryQuartzJobReq;
import com.infra.micro.quartz.model.vo.QuartzJobVO;
import com.infra.micro.quartz.domain.service.QuartzJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Quartz调度服务
 *
 * @author PD
 */
@RestController
@RequestMapping("/api/quartz/job")
@Api(tags = {"Quartz任务管理服务"})
public class QuartzJobController {
    @Resource
    private QuartzJobService quartzJobService;

    @ApiOperation(value = "创建作业", notes = "创建作业", produces = "application/json")
    @PostMapping
    public Response<Boolean> create(@RequestBody QuartzJobVO model) {
        return quartzJobService.create(model);
    }

    @ApiOperation(value = "根据ID更新作业", notes = "根据ID更新作业信息", produces = "application/json")
    @ApiImplicitParam(name = "jobId", value = "作业ID", dataType = "Long", dataTypeClass = Long.class, paramType = "path", required = true)
    @PutMapping("/{jobId}")
    public Response<Boolean> update(@PathVariable("jobId") Long jobId, @RequestBody QuartzJobVO model) {
        return quartzJobService.update(jobId, model);
    }

    @ApiOperation(value = "根据ID查询Job信息", notes = "通过JobId查询Job信息", produces = "application/json")
    @ApiImplicitParam(name = "jobId", value = "作业ID", dataType = "Long", dataTypeClass = Long.class, paramType = "path", required = true)
    @GetMapping("/{jobId}")
    public Response<QuartzJobVO> queryById(@PathVariable("jobId") Long jobId) {
        return quartzJobService.queryById(jobId);
    }

    @ApiOperation(value = "根据ID删除Job信息", notes = "通过JobId删除任务作业", produces = "application/json")
    @ApiImplicitParam(name = "jobId", value = "作业ID", dataType = "Long", dataTypeClass = Long.class, paramType = "path", required = true)
    @DeleteMapping(value = "/{jobId}")
    public Response<Boolean> deleteById(@PathVariable("jobId") Long jobId) {
        return Response.success(quartzJobService.deleteById(jobId));
    }

    @ApiOperation(value = "根据条件查询Job列表", notes = "分页查询已配置作业列表", produces = "application/json")
    @GetMapping("/list")
    public Response<Page<QuartzJobVO>> queryByPage(QueryQuartzJobReq page) {
        return quartzJobService.queryListByPage(page);
    }
}
